// pages/detail/detail.js
const app = getApp()
import _ from './../../utils/lodash.js';
import util from './../../utils/util.js';
const find = _.find;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    deviceId: '',
    name: '',
    connected: false,
    loading: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    console.log('options.name',options.name.length)
    this.setData({
      deviceId: options.deviceId ,
      name: options.name
    }, function () {
      this.createBLEConnection()
    })
    wx.setNavigationBarTitle({
      title: options.name,
    })
    const that = this
    wx.onBLEConnectionStateChange(function (res) {
      // 该方法回调中可以用于处理连接意外断开等异常情况
      console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
      that.setData({
        connected: res.connected
      })
    })
  },
  clickItem(e) {
    const ds = e.currentTarget.dataset
    const type = ds.type
    console.log(type)
    // wx.navigateTo({
    //   url: `/pages/detail/detail?deviceId=${this.data.deviceId}&name=${this.data.name}&type=${type}&connected=${this.data.connected?1:0}`
    // })
    // return
    if (this.data.connected||type==='位置信息') {
      wx.navigateTo({
        url: `/pages/detail/detail?deviceId=${this.data.deviceId}&name=${this.data.name}&type=${type}&connected=${this.data.connected?1:0}`
      })
    } else {
      util.toastError('请先连接设备');
    }

  },
  createBLEConnection() {
    console.log('createBLEConnection')
    this.setData({
      loading: true,
    })

    wx.getBLEMTU({
      deviceId: this.data.deviceId,
      writeType: 'write',
      success(res) {
        console.log('getBLEMTU')
        console.log(res)
      }
    })
    wx.createBLEConnection({
      deviceId: this.data.deviceId,
      success: (res) => {
        util.toastError('连接成功');
        console.log("createBLEConnection success")

        wx.setBLEMTU({
          deviceId: this.data.deviceId,
          mtu: 256,
          success: (res) => {
            console.log('设置MTU256成功')
          },
          fail: (res) => {
            console.log('设置MTU失败')
            console.log(res)
          },
        })
        this.setData({
          connected: true,
          loading: false,
        })

      },
      fail: (res) => {
        util.bleErrCode(res.errCode);
        console.log('连接失败')
        console.log(res)
        if (res.errCode === -1) {
          this.setData({
            connected: true,
            loading: false,
          })
        } else {
          this.setData({
            loading: false,
          })
        }

      }
    })

  },
  closeBLEConnection() {
    const that = this
    wx.closeBLEConnection({
      deviceId: this.data.deviceId,
      success: (res) => {
        console.log("closeBLEConnection success")
        util.toastError('断开连接成功');
        that.setData({
          connected: false
        })

      },
      fail: (res) => {
        console.log('断开连接失败')

        util.bleErrCode(res.errCode);
        console.log(res)
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('item onHIde')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('item onUnload')
    if(this.data.connected){
      this.closeBLEConnection()
    }

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh() {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})